import axios from "axios";
var domainUrl = window.location.origin;
if(window.location.origin=='http://localhost:8080'){
    domainUrl='https://mp.yile.vip/';//线上
    //domainUrl='http://tampa.mxth.com/';//测试
}
const mz = {
    ajax(opt) {
        const domain = domainUrl;
        let url = opt.url || '';
        let method = opt.method || 'GET';
        let data = opt.data || {};
        let responseType = opt.dataType || 'json';
        axios({
            url: url,
            baseURL: domain,
            method: method,
            params: data,
            header: {
                CacheControl: 'no-cache'
            }
        }).then(function (res) {
            if (res.status == 200) {
                if (res.data.result == "success") {
                    opt.success(res.data.data);
                } else {
                    console.log(res.data.errorMsg)
                }
            }
        }, function (err) {
            console.log(err)
            opt.error(err)
        })
    },
    post(opt) {
        const domain = domainUrl;
        let url = opt.url || '';
        let method = 'POST';
        let data = opt.data || {};
        let responseType = opt.dataType || 'json';
        axios({
            url: url,
            baseURL: domain,
            method: method,
            data: data,
        }).then(function (res) {
            if (res.status == 200) {
                opt.success(res.data);
            }
        }, function (err) {
            opt.error(err)
        })
    },
    get(opt) {
        const domain = domainUrl;
        let url = opt.url || '';
        let method = 'POST';
        let data = opt.data || {};
        let responseType = opt.dataType || 'json';
        let header = opt.header;
        var config = axios.create({
            baseURL: domain,
            method: method,
            headers: header
        })

        // header["cache-control"]='no-cache';
        config.get(url, { params: data }).then(function (res) {
            if (res.status == 200) {
                opt.success(res.data);
            }
        }, function (err) {
            opt.error(err)
        })
    },
    GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
}
export default mz;
